package com.gisuni.sddc.platformManager.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.model.Organization;
import com.gisuni.sddc.platformManager.model.RoleInfo;
import com.gisuni.sddc.platformManager.model.RoleRModule;
import com.gisuni.sddc.platformManager.model.RoleRResource;
import com.gisuni.sddc.util.StringUtil;

@IocBean
public class RoleDao {
	
	@Inject
	private Dao gisplatform;
//	private Dao dao;  
 	
 
 
    public Boolean executeSQL(String updateSql ){
    	
    	Boolean result = false;
    	Sql sql = Sqls.create(updateSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				return true;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(Boolean.class );
 
    }
 
 
	public String querySql(String  selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
			     while (rs.next()){
			    	 JSONObject ret=new JSONObject();
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
						}
			    	 result.put(ret);
			     }
				return result.toString();
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(String.class);
	}
	
	public String getControlRegion(String roleName){
		String result = "";
		if(!StringUtil.isNullOrEmpty(roleName)){
			 
			
		}
		return result;
	}


	public List<RoleInfo> findRole(String selectstr) {
		Sql sql = Sqls.create(selectstr);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				List<RoleInfo> result = new ArrayList<RoleInfo>();
			     while (rs.next()){
			    	 RoleInfo roleInfo=new RoleInfo();
			    	 if(rs.findColumn("ROLE_ID")>0) roleInfo.setRoleId(rs.getString("ROLE_ID"));
			    	 if(rs.findColumn("ROLE_PARENT_ID")>0) roleInfo.setRoleParentId(rs.getString("ROLE_PARENT_ID"));
			    	 if(rs.findColumn("ROLE_NAME")>0) roleInfo.setRoleName(rs.getString("ROLE_NAME"));
			    	 if(rs.findColumn("DESCRIPTION")>0) roleInfo.setDescription(rs.getString("DESCRIPTION"));
			    	 result.add(roleInfo);
			     }
			     
				return result;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(List.class);
	    
	}




	public String queryBaseRoleAll(int from, int to, String roleName) {
		Cnd cnd = null;;
		if(!StringUtil.isNullOrEmpty(roleName)){
			cnd = Cnd.where("ROLE_NAME","LIKE","%"+roleName+"%");
		}
		Pager pager = new Pager();
		pager.setPageNumber(from);
		pager.setPageSize(to);
		pager.setRecordCount(to);
		List<RoleInfo> list = gisplatform.query(RoleInfo.class, cnd, pager);
		if(list!=null && list.size()>0){
			JSONArray result=new JSONArray();
		    for(RoleInfo role : list){
		    	JSONObject ret=new JSONObject();
		    	ret.put("id", role.getRoleId());
		    	ret.put("name", role.getRoleName());
		    	ret.put("parentId", role.getRoleParentId());
		    	ret.put("type", role.getRoleType());
		    	ret.put("description", role.getDescription());
		    	ret.put("roleArea", role.getRoleArea());
		    	result.put(ret);
		    }
		    return result.toString();
		    	 
		}
		
		return null;
	}
	
	public int queryBaseRoleCount( String roleName) {
		Cnd cnd = null;;
		if(!StringUtil.isNullOrEmpty(roleName)){
			cnd = Cnd.where("ROLE_NAME","LIKE","%"+roleName+"%");
		}
		List<RoleInfo> listSec =gisplatform.query(RoleInfo.class, cnd, null);
		
		if(listSec!=null && listSec.size()>0){
			return listSec.size();
		}else{
			return 0;
		}
	}




	public boolean addRole(RoleInfo role) {
		RoleInfo t = gisplatform.insert(role);
		if(t!=null){
			return true;
		}
		return false;
	}




	public boolean updateRole(RoleInfo role) {
		if(gisplatform.updateIgnoreNull(role)>0)
			return true;
		return false;
	}




	public boolean delRole(RoleInfo role) {
		int res = gisplatform.delete(role);
		if(res>0){
			return true;
		}
		return false;
	}




	public RoleInfo findRoleById(String id) {
		RoleInfo role = new RoleInfo();
		role.setRoleId(id);
//		gisplatform.getEntity(org)
//		List<Record>  re  = gisplatform.query("ORGANIZATION", Cnd.where("ID","=",id));
		RoleInfo res = gisplatform.fetch(RoleInfo.class,id );
 
		return res;
	}


	public Boolean addRoleRModule(RoleRModule entity) {
		RoleRModule t = gisplatform.insert(entity);
		if(t!=null){
			return true;
		}
		return false;
		
	}


	public boolean addRoleRResource(RoleRResource entity) {
		RoleRResource t = gisplatform.insert(entity);
		if(t!=null){
			return true;
		}
		return false;
	}
	
}
